package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.BusinessInfo;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.product.api.TbLabelApi;
import com.cq.hd.product.api.dto.LabelSaveDto;
import com.cq.hd.product.api.vo.LabelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/businessActiveLabel")
@Api(tags = "活动标签相关接口")
public class BusinessActiveLabelController {

    @Resource
    private TbLabelApi labelApi;

    @ApiOperation(value = "推荐标签列表")
    @ApiResponse(response = LabelVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/recommends")
    public Result<List<LabelVo>> listRecommendLabels(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null) {
            Throw.isBusinessException("非商家账户");
        }
        return labelApi.listRecommendLabels(businessInfo.getId());
    }

    @ApiOperation(value = "标签搜索")
    @ApiResponse(response = LabelVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/search")
    public Result<List<LabelVo>> search(@ApiIgnore @LoginUser AppLoginUser appLoginUser, @RequestParam("keyword") String keyword) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null) {
            Throw.isBusinessException("非商家账户");
        }
        return labelApi.searchByClient(businessInfo.getId(), keyword);
    }

    @ApiOperation(value = "新增标签")
    @ApiImplicitParam(name = "labelSaveDto", value = "实体", required = true, paramType = "body", dataType = "LabelSaveDto")
    @PostMapping("/add")
    public Result<LabelVo> addLabel(@ApiIgnore @LoginUser AppLoginUser appLoginUser, @RequestBody LabelSaveDto labelSaveDto) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null) {
            Throw.isBusinessException("非商家账户");
        }
        labelSaveDto.setBusinessId(businessInfo.getId());
        return labelApi.saveLabel(labelSaveDto);
    }

}
